from sqlalchemy import create_engine, MetaData
from lib.config.mysql_config import mysql_config


def getdbconfig():
    config = mysql_config()
    host = config.host
    port = config.port
    user = config.user
    passwd = config.passwd
    db = config.db
    return "mysql+pymysql://{0}:{1}@{2}:{3}/{4}?charset=utf8".format(user, passwd, host, port, db)


def getdb():
    url = getdbconfig()
    #engine = create_engine(url, echo=True)
    engine = create_engine(url, echo=False)
    return engine
    # base = declarative_base()

#
# def gen():
#     metadata = MetaData()
#     # 反射数据库表结构
#     metadata.reflect(engine)
#
#     # 使用automap_base创建一个Base类，它会自动为所有表创建一个映射
#     Base = automap_base(metadata=metadata)
#     Base.prepare()
#     # 也可以这样：Base.prepare(engine, reflect=True)
#
#     # 将映射的表名与类关联
#     YourTable = Base.classes.users
#
#     # 创建会话
#     Session = sessionmaker(bind=engine)
#     session = Session()
#
#     # 从模型中查询数据
#     results = session.query(YourTable).all()
#
#     # 打印查询结果
#     # for result in results:
#     #     print(result)
